/**
 *******************************************************************************
 * @FileName  : smp_api.h
 * @Author    : GaoQiu
 * @CreateDate: 2020-02-18
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */

#ifndef SMP_API_H_
#define SMP_API_H_

#include "smp_defs.h"
#include "utils/mdc.h"
#include "utils/bd_addr.h"
#include "stack/ble_types.h"

typedef struct{
	uint8_t  ltk[16];
	uint8_t  rand[8];
	uint16_t EDIV;
}LTK_t;

/*! SMP error code define. */
enum{
	SMP_SUCCESS                       = 0x00,
	SMP_ERR_PASSKEY_ENTRY_FAILED      = 0x01, /*<! User input of passkey failed. */
	SMP_ERR_OOB_NOT_AVAILABLE         = 0x02, /*<! OOB data is not available.    */
	SMP_ERR_AUTHENTICATION_REQ        = 0x03, /*<! Authentication requirements cannot be met. */
	SMP_ERR_CONFIRM_VALUE_FAILED      = 0x04, /*<! Confirm value does not match. */
	SMP_ERR_PAIRING_NOT_SUPPORTED     = 0x05, /*<! Pairing is not supported by the device. */
	SMP_ERR_ENCRYTION_KEY_SIZE        = 0x06, /*<! Insufficient encryption key size. */
	SMP_ERR_COMMAND_NOT_SUPPORTED     = 0x07, /*<! Command not supported. */
	SMP_ERR_UNSPECIFIED_REASON        = 0x08, /*<! Unspecified reason. */
	SMP_ERR_REPEATED_ATTEMPTS         = 0x09, /*<! Repeated attempts   */
	SMP_ERR_INVALID_PARAM             = 0x0A, /*<! Invalid parameter or command length. */
	SMP_ERR_DHKEY_CHECK_FAILED        = 0x0B, /*<! DH Key check did not match.  */
	SMP_ERR_NUMERIC_COMPARISON_FAILED = 0x0C, /*<! Numeric comparison did not match.*/
	SMP_ERR_BR_EDR_PAIRING_IN_PROC    = 0x0D, /*<! BR/EDR cross transport key generation not allowed. */
	SMP_ERR_GENERATION_NOT_ALLOWED    = 0x0E,
};

#define SMP_ENCKEY      0x01
#define SMP_IDKEY       0x02
#define SMP_SIGN_KEY    0x04
#define SMP_LINK_KEY    0x08

/*! LTK data type. */
typedef struct{
  uint8_t              key[SMP_KEY_LEN];     /*!< LTK */
  uint8_t              rand[SMP_RAND8_LEN];  /*!< Rand */
  uint16_t             ediv;                 /*!< EDIV */
} SmpSecLtk_t;

/*! IRK data type. */
typedef struct{
  uint8_t              key[SMP_KEY_LEN];   /*!< IRK */
  bdAddr_t             bdAddr;             /*!< BD Address */
  uint8_t              addrType;           /*!< Address Type */
} SmpSecIrk_t;

/*! CSRK data type. */
typedef struct{
  uint8_t              key[SMP_KEY_LEN];   /*!< CSRK */
} SmpSecCsrk_t;

/*! Union of key types. */
typedef union{
  SmpSecLtk_t          ltk;   /*!< LTK */
  SmpSecIrk_t          irk;   /*!< IRK */
  SmpSecCsrk_t         csrk;  /*!< CSRK */
} SmpSecKey_t;

typedef struct{
	MdcMsgHdr_t   hdr;
	uint16_t      connHandle;
}SmpEvt_t;

/* Notify Application layer that Pair is finish */
typedef struct{
	MdcMsgHdr_t   hdr;
	uint16_t      connHandle;
	uint8_t       status;
}SmpPairEndInd_t;

/* Request Application layer to give a Pin Code */
typedef struct{
	MdcMsgHdr_t   hdr;
	uint16_t      connHandle;
}SmpPinCodeReq_t;

/* Notify Application layer that Pin Code is OK */
typedef struct{
	MdcMsgHdr_t   hdr;
	uint16_t      connHandle;
	uint32_t      pinCode;
}SmpPinCodeInd_t;

typedef void (*SmpEvtCback_t)(SmpEvt_t *pEvt);

void SMP_EvtRegister(SmpEvtCback_t evtCback);

void SMP_Init(void);

void SMP_RespInit(void);

void SMP_InitiatorInit(void);

void SMP_NonInit(void);

BleState_t SMP_SendSecReq(uint16_t connHandle, uint8_t auth);
void SMP_SetPinCode(uint16_t connHandle, uint32_t pinCode);

uint8_t *SMP_GetLtk(uint16_t connHandle, uint8_t *secLevel);
void SMP_DmPairEndHandler(uint16_t connHandle);

#endif /* SMP_API_H_ */
